<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <!-- <script src="/php/js/jquery1.9/jquery-1.9.0.min.js"></script> -->
    <?php
        $msg=$_REQUEST["msg"];
        require_once("DButil.php");
        $sql="select u_id from t_user where u_login_name='{$msg}'";
        $conn=getConn();
        $result = mysqli_query($conn, $sql);
        if($result){
            $u_id= (int) mysqli_fetch_assoc($result)['u_id'];//查询到名字对应的u_id
            // echo $u_id;
            $_SESSION["u_id"]=$u_id;//将登录名对应的id上传
        }
        mysqli_close($conn);
    ?>
    <script>
        var count=0;
        function doAdd(obj){
            // document.getElementById("add").onclick=function(){
                count++;
                // count=10;
            // }
            console.log(count);
            console.log(obj.parentNode.parentNode);
            tr=obj.parentNode.parentNode;//点击的当前行
            console.log(tr.children);
            tds=tr.children//当前行的所有td
            o_num=count;//总数量
            o_total=Number(tds[2].innerHTML) * count;//总金额 要乘数量
            console.log(o_total);
        }
    </script>
    <!-- 向订单表用添加数据 -->
            <?php
                // echo $o_total;
                require_once("DButil.php");
                $o_no= time() . mt_rand(100, 999);//订单编号 时间+随机数
                // $sql="insert into t_order (o_no,o_num,o_total,u_id) values('{$o_no}',o_num,o_total,{$u_id})";
                $conn=getConn();
                $result = mysqli_query($conn, $sql);
                if($result){
                    echo "<div>数据添加成功</div>";
                }else{
                    echo "error-msg:" . mysqli_error($conn);
                }
                mysqli_close($conn);
            ?>
</head>
<body>
    <!-- <h4>首页</h4> -->
    <div><span>用户名:<?=$msg?></span><span><a href="dingdan.php">我的订单</a></span></div>
    <!-- 成功获取登录的用户名 -->
    <div><span><a href="shouye.php">退出</a></span></div>
    <!-- 登录，注册 -->
    <a href="regPage.php">注册</a>
    <a href="loginPage.php">登录</a>

    <!-- ****************上架物品展示**************** -->
    <?php
    require_once("DButil.php");
    $sql="select p_id,p_name,p_price,p_unit,p_image_url,p_status from t_product where p_status=1";
    $conn=getConn();
    $result = mysqli_query($conn, $sql);
    // var_dump($result);
    if($result){
        // 使用 while
        echo "<table border='1' cellpadding='10' cellspacing='0' >
        <thead>
            <tr>
                <td>编号</td>
                <td>名称</td>
                <td>单价</td>
                <td>单位</td>
                <td>图片</td>
                <td>状态</td>
                <td>是否加入购物车</td>
            </tr>
        </thead>
        <tbody>";
        while($row = mysqli_fetch_assoc($result)) {
                // print_r($row);
                // echo "<br>";
                if($row['p_status']==1){
                    $row['p_status']="上架";
                }else{
                    $row['p_status']="下架";
                }
            echo "<tr>
                <td>{$row['p_id']}</td>
                <td>{$row['p_name']}</td>
                <td>{$row['p_price']}</td>
                <td>{$row['p_unit']}</td>
                <td><img src=\"{$row['p_image_url']}\" style=\"width: 100px;\" alt=\"no image\"></td>
                <td>{$row['p_status']}</td>
                <td><input id='add' type='button' value='加入购物车' onClick='doAdd(this)'></td>
            </tr>";
        }
        echo "</tbody></table>";
    }
    mysqli_close($conn);
    ?>
    <!-- <input type="button" value="购物车"> -->
</body>
</html>